package cn.wolfcode.controller;


import cn.wolfcode.entity.HisNew;
import cn.wolfcode.entity.Hisrinkinglist;
import cn.wolfcode.entity.NewContent;
import cn.wolfcode.entity.News;
import cn.wolfcode.qo.NewsQuery;
import cn.wolfcode.redis.service.INewsRedisService;
import cn.wolfcode.service.IHisNewService;
import cn.wolfcode.service.IHisrinkinglistService;
import cn.wolfcode.service.INewContentService;
import cn.wolfcode.service.INewsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dou
 * @since 2023-04-13
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {
@Autowired
private IHisNewService hisNewService;
    @Autowired
    private INewsService newsService;
    @Autowired
    private INewContentService newContentService;
    @Autowired
    private INewsRedisService newsRedisService;

    @Autowired
    private IHisrinkinglistService hisrinkinglistService;

    @RequestMapping("/page")
    public Page<News> page(NewsQuery qo) {

        return newsService.queryPage(qo);
    }

    @RequestMapping("/detail")
    public News detail(Long id) {
        News news = newsService.getById(id);
        NewContent content = newContentService.getById(id);
        news.setContent(content);
        /**-
         * 阅读数+1
         */
        long viewnum = newsRedisService.viewnumIncr(id);
        news.setViewnum(viewnum);
        chijiuhua();
        return news;
    }

    public boolean chijiuhua() {
        return newsRedisService.chijiuhua();
    }



    //访问当前请求到新的排行榜
    @RequestMapping("/createRin")
    public Page<News> createRin() {
        //创建排行榜Hisrinkinglist对象
        //当前时间
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        Hisrinkinglist hisrinkinglist = new Hisrinkinglist();
        hisrinkinglist.setName(dateString + "排行榜");
        hisrinkinglist.setImplTime(new Date());
//向数据库新添排行榜
        hisrinkinglistService.insert(hisrinkinglist);
        //找到hisrinkinglist的创建时间与文章的创建时间进行操作
        Hisrinkinglist rinking = hisrinkinglistService.getById(hisrinkinglist.getId());
        //排行榜创建时间
        Date implTime = rinking.getImplTime();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = implTime.toInstant().atZone(zoneId).toLocalDateTime();
        //获取所有文章
        List<HisNew> list = new ArrayList<>();

        for (News news : newsService.list()) {
            //获取文章访问量
            Long viewnum = news.getViewnum();
            //文章创建时间
            LocalDateTime createTime = news.getCreateTime();
            int i = localDateTime.getDayOfYear() - createTime.getDayOfYear();
            double count = 0.0;

            switch (i) {
                case 0:
                    count = viewnum * 1;
                    break;
                case 1:
                    count = viewnum * 0.9;
                    break;
                case 2:
                    count = viewnum * 0.8;
                    break;
                case 3:
                    count = viewnum * 0.7;
                    break;
                case 4:
                    count = viewnum * 0.6;
                    break;
                case 5:
                    count = viewnum * 0.5;
                    break;
                case 6:
                    count = viewnum * 0.4;
                    break;
                case 7:
                    count = viewnum * 0.3;
                    break;
                default:
                    count = 0;
                    break;

            }


            HisNew hisNew = new HisNew(rinking.getId(), news.getId(), count);
            list.add(hisNew);
        }
        Collections.sort(list, new Comparator<HisNew>() {
            @Override
            public int compare(HisNew o1, HisNew o2) {
                return (int) (o2.getCount()-o1.getCount());
            }
        });
        System.out.println(list);
        for (HisNew hisNew : list) {
            hisNew.insert();
        }
        //获取文章的
        return list(hisrinkinglist.getId());


    }
    //通过中间表查询表出文章的顺序
@RequestMapping("/rinking")
    public Page<News> list(Long hisId){
       if (hisId==null){
           return createRin();
       }
        //通过中间表查询文章id
        List<News> news =newsService.getByHisId(hisId);
    NewsQuery qo = new NewsQuery();
    Page<News> newsPage = newsService.queryPage(qo);
    newsPage.setRecords(news);
        return newsPage;
    }

    @RequestMapping("/hispage")
    public Page<Hisrinkinglist> hispage(NewsQuery qo) {

        return hisrinkinglistService.queryPage(qo);
    }

}

